package com.shanjupay.merchant.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shanjupay.merchant.service.api.MerchantService;
import com.shanjupay.merchant.service.entity.MerchantEntity;
import com.shanjupay.merchant.mapper.MerchantMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shanjupay.merchant.service.dto.MerchantDTO;
import com.shanjupay.merchant.service.param.MerchantParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;

/**
 * @author xiams
 * @date 2021-05-19
 */

@Service
@Slf4j
public class MerchantServiceImpl extends ServiceImpl<MerchantMapper, MerchantEntity> implements MerchantService {

    @Override
    public MerchantDTO queryMerchantTest(Long id) {
        LambdaQueryWrapper<MerchantEntity> wrapper = Wrappers.lambdaQuery(MerchantEntity.class).eq(MerchantEntity::getId, id);
        MerchantEntity merchantEntity = getOne(wrapper);
        MerchantDTO dto = new MerchantDTO();
        BeanUtil.copyProperties(merchantEntity, dto);
        log.info("调用测试流程：【{}】", dto);
        return dto;
    }

    @Override
    public void register(MerchantParam param) {
        LambdaQueryWrapper<MerchantEntity> wrapper = Wrappers.lambdaQuery(MerchantEntity.class).eq(MerchantEntity::getPhone, param.getPhone());
        Assert.isTrue(count(wrapper) <= 0, "该商户手机号码已在平台注册,请转向登录页！");
        MerchantEntity entity = new MerchantEntity();
        entity.setPhone(param.getPhone());
        entity.setAuditStatus(0);
        save(entity);

    }

}
